﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StudentPlanRepository.cs" company="">
//   Copyright (c) 2010/2011 All Right Reserved
// </copyright>
// <summary>
//   Contains StudentPlanRepository class declaration.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace iBuproModel.Repositories.Classes
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using iBuproModel.Repositories.Interfaces;

    /// <summary>
    /// Implements IStudentPlanRepository interface using linq to entities.
    /// </summary>
    public class StudentPlanRepository : GenericRepository<StudentPlan>, IStudentPlanRepository
    {
        public new void Update(StudentPlan plan)
        {
            var old = context.StudentPlans.FirstOrDefault(p => p.Id == plan.Id);
            if (old != null)
            {
                old.Name = plan.Name;
                old.Shortcut = plan.Shortcut;
                context.SaveChanges();
            }
            else throw new Exception("Updated studentplan doesn't exists");
        }

        public IEnumerable<Subject> GetSubjectsForStudentPlan(StudentPlan studentplan)
        {
            return studentplan.Subjects.ToList();
        }

        public void UpdateSubjectList(StudentPlan plan, IEnumerable<int> ids)
        {
            
            plan.Subjects.Clear();

            foreach (int id in ids)
            {
                plan.Subjects.Add(context.Subjects.First(s => s.Id == id));
            }
        }
        
        public IEnumerable<StudentField> GetStudentFieldsForStudentPlan(StudentPlan studentplan)
        {
            return studentplan.StudentFields.ToList();
        }

        public void UpdateStudentFieldList(StudentPlan plan, IEnumerable<int> ids)
        {

            plan.StudentFields.Clear();

            foreach (int id in ids)
            {
                plan.StudentFields.Add(context.StudentFields.First(s => s.Id == id));
            }
        }


        public void RemoveSubjects(StudentPlan plan)
        {
            plan.Subjects.Clear();
        }

       public void RemoveStudentFields(StudentPlan plan)
        {
            plan.Subjects.Clear();
        }
    }
}
